package com.zhouyu;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.List;
import java.util.stream.Collectors;

public class JarClassFinder {
	public static void main(String[] args) throws IOException, ClassNotFoundException {
        String jarPath = "C:\\Users\\CAI\\.m2\\repository\\io\\lettuce\\lettuce-core\\5.1.4.RELEASE\\lettuce-core-5.1.4.RELEASE.jar";
        String basePackage = "org.springframework.core";
        List<Class<?>> classes = findClassesInJar(jarPath, basePackage);
        System.out.println(classes.stream().map(Class::getName).collect(Collectors.toList()));
    }
    public static List<Class<?>> findClassesInJar(String jarPath, String basePackage) throws IOException, ClassNotFoundException {
        List<Class<?>> classes = new     ArrayList<>();
        JarFile jar = new JarFile(jarPath);
        Enumeration<JarEntry> entries = jar.entries();

        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
            String entryName = entry.getName();
            if (entryName.endsWith(".class") && entryName.startsWith(basePackage)) {
                String className = entryName.substring(0, entryName.length() - 6).replace('/', '.');
                classes.add(Class.forName(className));
            }
        }

        jar.close();
        return classes;
    }
}
